package cate.game.status;

import cate.common.game.GameCollection;
import cate.game.GameBody;
import cp.solution.util.time.DateKit;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Date;

@Document(collection = GameCollection.NAME_STATUS)
public class GameStatus {

    public static final String AUTO_DELETE_FIELD = "saveTime";

    @NoteField("主键")
    @Id
    public String id;

    @NoteField(value = "测试笔记")
    public String note;

    @NoteField(value = "版本")
    public int version;

    @NoteField(value = "累计创建角色数量统计")
    public int masterCreateNum;

    @NoteField(value = "服务器第一次运行时间", detail = "并不精准")
    public long firstRunTime;

    @NoteField(value = "开服时间")
    public long openTime;

    @NoteField(value = "合服时间")
    public long mergeTime;

    @NoteField(value = "保存时间")
    public Date saveTime;

    @NoteField(value = "上次跨天时间")
    public long lastDaySpanTime;

    @NoteField(value = "上次跨周时间")
    public long lastWeekSpanTime;

    @NoteField(value = "上次跨月时间")
    public long lastMonthSpanTime;

    @NoteField(value = "用于开发调试的时间偏移ms")
    public long timeOffset;

    public void initialize(){
        // 初始化跨天、跨周、跨月时间
        if (0L == lastDaySpanTime){
            lastDaySpanTime = System.currentTimeMillis();
        }
        if (0L == lastWeekSpanTime){
            lastWeekSpanTime = System.currentTimeMillis();
        }
        if (0L == lastMonthSpanTime){
            lastMonthSpanTime = System.currentTimeMillis();
        }
    }

    /** 开服天数 */
    @JsonIgnore
    public int getOpenDay(GameBody game) {
        // 开服时间如果在未来 那就算第0天
        if (this.openTime > game.time.now()) {
            return 0;
        }
        return DateKit.getDistanceDays(this.openTime, game.time.now()) + 1;
    }
}
